package com.loveweinuo;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.loveweinuo.bean.RegisterCallBackBean;
import com.loveweinuo.databinding.ActivityMainBinding;
import com.loveweinuo.util.GsonUtil;
import com.loveweinuo.util.HTTPAPI;
import com.loveweinuo.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        binding.tvModuleLogin.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvModuleLogin:
                String url = binding.etModuleUrl.getText().toString().trim();
                if (TextUtils.isEmpty(url)) {
                    ToastUtil.showToast("请输入url");
                    return;
                }

                String phone = binding.etModulePhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入phone");
                    return;
                }

                String pwd = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                String registerId = "";

                login(url, phone, pwd, registerId);
                break;
            default:
        }
    }

    public void login(String url, String phone, String pwd, String registerId) {
        HTTPAPI.getInstance().login(url, phone, pwd, registerId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ToastUtil.showToast("登陆成功-->" + s);
                binding.tvModuleResult.setText(s);
                RegisterCallBackBean bean = GsonUtil.GsonToBean(s, RegisterCallBackBean.class);
                if (!"200".equals(bean.getCode())) {
                    ToastUtil.showToast("登陆失败-->"+ bean.getMsg());
                    return;
                }
                Intent intent = new Intent(MainActivity.this,PayActivity.class);
                intent.putExtra("module_token",bean.getResult().getToken());
                intent.putExtra("module_userId",bean.getResult().getUserId());
                startActivity(intent);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                binding.tvModuleResult.setText(response.body().toString());
                ToastUtil.showToast("登陆失败-->" + e.getMessage());
            }
        });
    }
}
